import sys
import os
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../../'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/landscape/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/landscape/simulator/'))
import numpy as np
import qiskit
provider = qiskit.IBMQ.load_account()
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit.algorithms import QAOA
from shared.QiskitMaxcut import *
from ibm.ibm_parameters import *
from matplotlib import pyplot as plt
%matplotlib inline
from ibm_landscape_processes import *
ibmqfactory.load_account:WARNING:2021-08-29 15:41:40,159: Credentials are already in use. The existing account in the session will be replaced.
%load_ext autoreload
%autoreload 2
# ---- Define graph and MaxCut ----
graph = generate_butterfly_graph(with_weights=True)
opt_max_cut = -35
max_cut = Maxcut(graph, opt_max_cut)
max_cut_qubo = max_cut.to_qubo()
max_cut.draw()
from landscape_helper import *
landscape = load_landscape_data('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/landscape/simulator/landscape_simulator_butterfly_weights_results.npy')
min_gamma, min_beta, min_exp = describe_landscape(landscape)
Landscape mean: -23.56 Min Exp.Value: -32.36 Min. Gamma: -2.34, Min. Beta: -2.94
from plot_helper import *
# Plot landscape in 3D
plot_landscape_3d(landscape)
# Plot Heatmap
heatmap = plot_heatmap(landscape)
heatmap = display_minimum(heatmap, min_gamma, min_beta, min_exp)
from tqa import calculate_tqa
p = 1
tqa_initial_points = calculate_tqa(graph, p)
TQA: Gamma: [0.45175879], Beta: [0.45175879] (p=1)
from qaoa_helper import *
qnSpsa = QNSPSA(calculate_fidelity(create_qaoa(), max_cut_qubo), maxiter=50)
optimizers = [
('COBYLA',COBYLA(maxiter=50)),
('SPSA', SPSA(maxiter=50)),
('QNSPSA', qnSpsa),
]
for optimizer_tuple in optimizers:
optimizer_name, optimizer = optimizer_tuple
# create qaoa
qaoa = create_qaoa(optimizer = optimizer,
reps=p,
initial_point=tqa_initial_points,
with_callback=True
)
# run qaoa
result, optimal_parameters, optimizer_history = run_qaoa_with_callback(qaoa, max_cut_qubo)
# analyse results
print(f"{optimizer_name} Optimizer")
mean, distribution = max_cut.analyse(result, print_output=True)
max_cut.plot_histogram(distribution, mean)
optimizer_history[2][-1] = mean
print()
# Display Optimizer Results
counts, energy_values, maxcut_values, optimizer_gammas, optimizer_betas = optimizer_history
display_optimizer_path(heatmap, optimizer_gammas, optimizer_betas, maxcut_values, optimizer_name)
# Plot Optimizer History MaxCut Evaluation # Values from landscape -> real values may deviate
plot_optimizer_maxcut_history(counts, maxcut_values, optimizer_name)
# Plot Optimizer History Energy Evaluation -> not MaxCutMean!
plot_optimizer_energy_history(counts, energy_values, optimizer_name)
COBYLA Optimizer optimal function value: -35.0 optimal value: [0. 1. 0. 1. 0.] status: SUCCESS Number of samples (31) is too large to display. Skip. Expectation Value: -28.779250000000005 Highest Exp.Value: 0 with 2.8875 % Lowest Exp.Value: -35 with 1.2 % Highest Probability: -32 with 29.4 % Ratio r: 0.8222642857142859 Approiximation ratio: 0.8222642857142859 MSE: 135.80030748557698 RMSE: 11.653338898597989
SPSA Optimizer optimal function value: -35.0 optimal value: [0. 1. 0. 1. 0.] status: SUCCESS Number of samples (32) is too large to display. Skip. Expectation Value: -24.189750000000004 Highest Exp.Value: 0 with 5.7 % Lowest Exp.Value: -35 with 6.2875 % Highest Probability: -32 with 18.038 % Ratio r: 0.6911357142857144 Approiximation ratio: 0.6911357142857144 MSE: 107.34646660096153 RMSE: 10.360813993164896
QNSPSA Optimizer optimal function value: -35.0 optimal value: [0. 1. 0. 1. 0.] status: SUCCESS Number of samples (32) is too large to display. Skip. Expectation Value: -28.474125000000008 Highest Exp.Value: 0 with 2.4 % Lowest Exp.Value: -35 with 3.175 % Highest Probability: -32 with 29.763 % Ratio r: 0.8135464285714288 Approiximation ratio: 0.8135464285714288 MSE: 132.60133297716354 RMSE: 11.515265215233367
eval_num = 100
max_p = 10
all_results = {}
from qaoa_helper import *
qaoa_p_means = []
qaoa_p_ratios = []
qaoa_p_approx_ratios = []
for p in range(1,max_p+1):
m, r, ar = start_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p)
qaoa_p_means.append(m)
qaoa_p_ratios.append(r)
qaoa_p_approx_ratios.append(ar)
all_results["QAOA"] = [qaoa_p_means, qaoa_p_ratios, qaoa_p_approx_ratios]
p=1: .................................................................................................... p=2: .................................................................................................... p=3: .................................................................................................... p=4: .................................................................................................... p=5: .................................................................................................... p=6: .................................................................................................... p=7: .................................................................................................... p=8: .................................................................................................... p=9: .................................................................................................... p=10: ....................................................................................................
display_boxplots_results(qaoa_p_means, qaoa_p_ratios, qaoa_p_approx_ratios)
from qaoa_helper import *
tqa_p_means = []
tqa_p_ratios = []
tqa_p_approx_ratios = []
tqa_init_points = []
for p in range(1,max_p+1):
tqa_initial_points = calculate_tqa(graph, p)
m, r, ar = start_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p, init_points=tqa_initial_points)
tqa_p_means.append(m)
tqa_p_ratios.append(r)
tqa_p_approx_ratios.append(ar)
tqa_init_points.append(tqa_initial_points)
all_results["TQA QAOA"] = [tqa_p_means, tqa_p_ratios, tqa_p_approx_ratios, tqa_init_points]
TQA: Gamma: [0.45175879], Beta: [0.45175879] (p=1) p=1: .................................................................................................... TQA: Gamma: [0.26237437 0.78712312], Beta: [0.78712312 0.26237437] (p=2) p=2: .................................................................................................... TQA: Gamma: [0.19173646 0.57520938 0.9586823 ], Beta: [0.9586823 0.57520938 0.19173646] (p=3) p=3: .................................................................................................... TQA: Gamma: [0.125 0.375 0.625 0.875], Beta: [0.875 0.625 0.375 0.125] (p=4) p=4: .................................................................................................... TQA: Gamma: [0.08 0.24 0.4 0.56 0.72], Beta: [0.72 0.56 0.4 0.24 0.08] (p=5) p=5: .................................................................................................... TQA: Gamma: [0.05555556 0.16666667 0.27777778 0.38888889 0.5 0.61111111], Beta: [0.61111111 0.5 0.38888889 0.27777778 0.16666667 0.05555556] (p=6) p=6: .................................................................................................... TQA: Gamma: [0.04081633 0.12244898 0.20408163 0.28571429 0.36734694 0.44897959 0.53061224], Beta: [0.53061224 0.44897959 0.36734694 0.28571429 0.20408163 0.12244898 0.04081633] (p=7) p=7: .................................................................................................... TQA: Gamma: [0.03125 0.09375 0.15625 0.21875 0.28125 0.34375 0.40625 0.46875], Beta: [0.46875 0.40625 0.34375 0.28125 0.21875 0.15625 0.09375 0.03125] (p=8) p=8: .................................................................................................... TQA: Gamma: [0.02469136 0.07407407 0.12345679 0.17283951 0.22222222 0.27160494 0.32098765 0.37037037 0.41975309], Beta: [0.41975309 0.37037037 0.32098765 0.27160494 0.22222222 0.17283951 0.12345679 0.07407407 0.02469136] (p=9) p=9: .................................................................................................... TQA: Gamma: [0.02 0.06 0.1 0.14 0.18 0.22 0.26 0.3 0.34 0.38], Beta: [0.38 0.34 0.3 0.26 0.22 0.18 0.14 0.1 0.06 0.02] (p=10) p=10: ....................................................................................................
display_boxplots_results(tqa_p_means, tqa_p_ratios, tqa_p_approx_ratios, prefix='TQA ')
from warmstart_helper import *
ws_p_means = []
ws_p_ratios = []
ws_p_approx_ratios = []
ws_opt_epsilons = []
for p in range(1,max_p+1):
opt_epsilon = 0.25 #optimize_epsilon(max_cut, reps=p)
m, r, ar = start_ws_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p, epsilon=0.25)
ws_p_means.append(m)
ws_p_ratios.append(r)
ws_p_approx_ratios.append(ar)
ws_opt_epsilons.append(opt_epsilon)
all_results["WarmStart QAOA"] = [ws_p_means, ws_p_ratios, ws_p_approx_ratios, ws_opt_epsilons]
p=1: .................................................................................................... p=2: .................................................................................................... p=3: .................................................................................................... p=4: .................................................................................................... p=5: .................................................................................................... p=6: .................................................................................................... p=7: .................................................................................................... p=8: .................................................................................................... p=9: .................................................................................................... p=10: ....................................................................................................
display_boxplots_results(ws_p_means, ws_p_ratios, ws_p_approx_ratios, prefix='WarmStart ')
from recursive_qaoa_helper import *
recursive_p_means = []
recursive_p_ratios = []
recursive_p_approx_ratios = []
for p in range(1,max_p+1):
m, r, ar = start_recursive_evaluation(max_cut, eval_num=eval_num, reps=p)
recursive_p_means.append(m)
recursive_p_ratios.append(r)
recursive_p_approx_ratios.append(ar)
all_results["Recursive QAOA"] = [recursive_p_means, recursive_p_ratios, recursive_p_approx_ratios]
p=1: .................................................................................................... p=2: .................................................................................................... p=3: .................................................................................................... p=4: .................................................................................................... p=5: .................................................................................................... p=6: .................................................................................................... p=7: .................................................................................................... p=8: .................................................................................................... p=9: .................................................................................................... p=10: ....................................................................................................
display_boxplots_results(recursive_p_means, recursive_p_ratios, recursive_p_approx_ratios, prefix='Recursive ')
from recursive_ws_helper import *
ws_recursive_p_means = []
ws_recursive_p_ratios = []
ws_recursive_p_approx_ratios = []
for p in range(1,max_p+1):
opt_epsilon = ws_opt_epsilons[p-1]
m, r, ar = start_recursive_ws_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p, epsilon=opt_epsilon)
ws_recursive_p_means.append(m)
ws_recursive_p_ratios.append(r)
ws_recursive_p_approx_ratios.append(ar)
all_results["Recursive WarmStrart QAOA"] = [ws_recursive_p_means, ws_recursive_p_ratios, ws_recursive_p_approx_ratios, ws_opt_epsilons]
p=1: .................................................................................................... p=2: .................................................................................................... p=3: .................................................................................................... p=4: .................................................................................................... p=5: .................................................................................................... p=6: .................................................................................................... p=7: .................................................................................................... p=8: .................................................................................................... p=9: .................................................................................................... p=10: ....................................................................................................
display_boxplots_results(ws_recursive_p_means, ws_recursive_p_ratios, ws_recursive_p_approx_ratios, prefix='Recursive WarmStart ')
# Save results
with open(f'comparison_simulator_{graph.name}_no_epsilon_results.npy', 'wb') as f:
np.save(f, all_results)
from results_helper import *
means_df, ratio_df, approx_ratios_df = generate_dataframes(all_results)
means_df
| QAOA | TQA QAOA | WarmStart QAOA | Recursive QAOA | Recursive WarmStrart QAOA | |
|---|---|---|---|---|---|
| p | |||||
| 1 | -30.06 | -28.77 | -29.42 | -34.49 | -34.61 |
| 2 | -30.68 | -29.10 | -29.45 | -34.65 | -34.57 |
| 3 | -30.96 | -31.67 | -29.63 | -34.73 | -34.57 |
| 4 | -31.06 | -31.98 | -29.46 | -34.75 | -34.67 |
| 5 | -30.93 | -30.57 | -29.46 | -34.84 | -34.65 |
| 6 | -30.62 | -30.57 | -29.30 | -34.84 | -34.65 |
| 7 | -30.44 | -31.55 | -28.89 | -34.76 | -34.66 |
| 8 | -30.19 | -29.78 | -28.83 | -34.68 | -34.66 |
| 9 | -29.85 | -31.67 | -28.58 | -34.67 | -34.64 |
| 10 | -30.00 | -29.42 | -28.35 | -34.57 | -34.51 |
ratio_df
| QAOA | TQA QAOA | WarmStart QAOA | Recursive QAOA | Recursive WarmStrart QAOA | |
|---|---|---|---|---|---|
| p | |||||
| 1 | 0.86 | 0.82 | 0.84 | 0.0 | 0.0 |
| 2 | 0.87 | 0.83 | 0.84 | 0.0 | 0.0 |
| 3 | 0.88 | 0.90 | 0.85 | 0.0 | 0.0 |
| 4 | 0.89 | 0.91 | 0.84 | 0.0 | 0.0 |
| 5 | 0.88 | 0.87 | 0.84 | 0.0 | 0.0 |
| 6 | 0.87 | 0.87 | 0.84 | 0.0 | 0.0 |
| 7 | 0.87 | 0.90 | 0.83 | 0.0 | 0.0 |
| 8 | 0.86 | 0.85 | 0.82 | 0.0 | 0.0 |
| 9 | 0.85 | 0.90 | 0.82 | 0.0 | 0.0 |
| 10 | 0.86 | 0.84 | 0.81 | 0.0 | 0.0 |
approx_ratios_df
| QAOA | TQA QAOA | WarmStart QAOA | Recursive QAOA | Recursive WarmStrart QAOA | |
|---|---|---|---|---|---|
| p | |||||
| 1 | 0.86 | 0.82 | 0.84 | 0.99 | 0.99 |
| 2 | 0.88 | 0.83 | 0.84 | 0.99 | 0.99 |
| 3 | 0.88 | 0.90 | 0.85 | 0.99 | 0.99 |
| 4 | 0.89 | 0.91 | 0.84 | 0.99 | 0.99 |
| 5 | 0.88 | 0.87 | 0.84 | 1.00 | 0.99 |
| 6 | 0.87 | 0.87 | 0.84 | 1.00 | 0.99 |
| 7 | 0.87 | 0.90 | 0.83 | 0.99 | 0.99 |
| 8 | 0.86 | 0.85 | 0.82 | 0.99 | 0.99 |
| 9 | 0.85 | 0.90 | 0.82 | 0.99 | 0.99 |
| 10 | 0.86 | 0.84 | 0.81 | 0.99 | 0.99 |